package com.shopping.mall.core.module.freemarkerMail.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopping.mall.core.module.freemarkerMail.entity.FreemarkerMail;
import com.shopping.mall.core.module.freemarkerMail.mapper.FreemarkerMailMapper;
import com.shopping.mall.core.module.freemarkerMail.service.FreemarkerMailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopping.mall.base.http.ResponseState;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.base.util.WrapperUtils;
import com.shopping.mall.core.module.mainPage.entity.MainPage;
import io.jsonwebtoken.ExpiredJwtException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
 * <p>
 *  FreemarkerMail服务实现类
 * </p>
 *
 * @author zzy
 * @since 2021-09-27
 */
@Validated
@Service
public class FreemarkerMailServiceImpl extends ServiceImpl<FreemarkerMailMapper, FreemarkerMail> implements FreemarkerMailService {

    @Autowired
    private FreemarkerMailMapper freemarkerMailMapper;

    /**
     * 添加模板
     * @param template
     * @param mailType
     * @return
     */
    @Override
    public Result<Object> add(String template,Integer mailType) {
        FreemarkerMail freemarkerMail=new FreemarkerMail();
        freemarkerMail.setTemplate(template);
        freemarkerMail.setMailType(mailType);
        final int insert = freemarkerMailMapper.insert(freemarkerMail);
        if (insert==1){
            return Result.OK();
        }else {
            return Result.error(ResponseState.ERROR.getMessage());
        }
    }

    /**
     * 返回最新的模板
     *
     * @return
     */
    @Override
    public Result<Object> select() {
        final List<FreemarkerMail> freemarkerMail = freemarkerMailMapper.lastData();
        return Result.OK(freemarkerMail);
    }

    /**
     * 分页查询
     * @param current：页码
     * @param size：每页条数
     * @param freemarkerMail：筛选条件
     * @return IPage<FreemarkerMail>
     */
    @Override
    public IPage<FreemarkerMail> getFreemarkerMailPage(Integer current,Integer size,FreemarkerMail freemarkerMail){
        QueryWrapper<FreemarkerMail> queryWrapper = WrapperUtils.queryWrapper(freemarkerMail);
        Page<FreemarkerMail> freemarkerMailPage = new Page<>(current,size);
        IPage<FreemarkerMail> freemarkerMailIPage = freemarkerMailMapper.selectPage(freemarkerMailPage, queryWrapper);
        return freemarkerMailIPage;
    }

    /**
     * 查询所有
     * @param freemarkerMail：筛选条件
     * @return List<FreemarkerMail>
     */
    @Override
    public List<FreemarkerMail> getAll(FreemarkerMail freemarkerMail){
        QueryWrapper<FreemarkerMail> queryWrapper = WrapperUtils.queryWrapper(freemarkerMail);
        return freemarkerMailMapper.selectList(queryWrapper);
    }

    /**
     * 通过id删除
     * @param id：freemarkerMailId
     * @return Result<Object>
     */
    @Override
    public Result<Object> deleteById(String id){
        FreemarkerMail freemarkerMail=freemarkerMailMapper.selectById(id);
        if(freemarkerMail==null){
            return Result.error(ResponseState.TARGET_NOT_EXIST.getValue(), ResponseState.TARGET_NOT_EXIST.getMessage());
        }
        freemarkerMailMapper.deleteById(id);
        return Result.OK();
    }

    /**
    * 批量删除
    * @param ids：id列表
    * @return Result<Object>
    */
    @Override
    public Result<Object> batchDelete(List<String> ids){
        UpdateWrapper<FreemarkerMail> updateWrapper=new UpdateWrapper<>();
        updateWrapper.in("id",ids);
        freemarkerMailMapper.delete(updateWrapper);
        return Result.OK();
    }
}